//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <bsp.h>

#define RDCR    0x80900010//Address of register for year, month and day of month
#define RYCR    0x80900014//Address of register for hour, minute, second and
                          // day of week

EXTERN_C void CDECL BspGetRTCTime(RTCTime *pRTCTime)
{
    uint32_t uYMD = Inl(RYCR);
    uint32_t uHMS = Inl(RDCR);

    pRTCTime->uYear     = (uYMD >> 9) & 0xfff;//Year
    pRTCTime->uMonth    = (uYMD >> 5) & 0xf;//Month
    pRTCTime->uDate     = (uYMD & 0x1f);//Day of Month

    pRTCTime->uHours    = (uHMS >> 12) & 0x1f;//Hour
    pRTCTime->uMinutes  = (uHMS >> 6) & 0x3f;//Minnute
    pRTCTime->uSeconds  = (uHMS & 0x3f);//Second

    //pRTCTime->uWeekday     = (uHMS >> 17) & 0x3;//Day of Week
}

EXTERN_C void CDECL BspSetRTCTime(const RTCTime *pRTCTime)
{
    //Valid day of week
//    if (pRTCTime->uWeekday < 1 || pRTCTime->uWeekday > 7)
//        pRTCTime->uWeekday = 1;

    uint32_t uYMD = 0;
    uint32_t uHMS = 0;

    uYMD = ((pRTCTime->uYear & 0xfff) << 9) | ((pRTCTime->uMonth & 0xf) << 5)
           | (pRTCTime->uDate & 0x1f);
    uHMS = ((pRTCTime->uHours & 0x1f) << 12) | ((pRTCTime->uMinutes&0x3f) << 6)
           | (pRTCTime->uSeconds & 0x3f);

//    uHMS |= ((pRTCTime->uWeekday & 0x3) << 17);

    Outl(RYCR, uYMD);
    Outl(RDCR, uHMS);
}
